﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics
{
    /// <summary>
    /// 
    /// </summary>
    public enum Colorable
    {
        Red,
        Green,
        Blue,
        Yellow,
        Black,
        White,
        Cyan,
        Magenta,
        Gray,
        None
    }

    /// <summary>
    /// 
    /// </summary>
    public class ImageFilters
    {
        /// <summary>
        /// Fasts the fourier transformation.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <returns></returns>
        public static Bitmap FastFourierTransformation(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Bitmap bitmap = new Bitmap(w, h);
            Color pixel;
            for (int x = 0; x < w; x++)
            {
                PointD[] r = new PointD[h];
                PointD[] g = new PointD[h];
                PointD[] b = new PointD[h];
                PointD[] rf = new PointD[h];
                PointD[] gf = new PointD[h];
                PointD[] bf = new PointD[h];
                for (int y = 0; y < h; y++)
                {
                    if (x < lw && y < lh)
                    {
                        pixel = MyBitmap.GetPixel(x, y);
                        r[y].x = pixel.R;
                        g[y].x = pixel.G;
                        b[y].x = pixel.B;
                    }
                }
                Maths.FastFourierTransformation(r, rf, hp);
                Maths.FastFourierTransformation(g, gf, hp);
                Maths.FastFourierTransformation(b, bf, hp);

                for (int y = 0; y < h; y++)
                    bitmap.SetPixel(x, y, Color.FromArgb((int)rf[y].x, (int)gf[y].x, (int)bf[y].x));
            }
            Bitmap bitmap1 = new Bitmap(w, h);
            for (int y = 0; y < h; y++)
            {
                PointD[] r = new PointD[w];
                PointD[] g = new PointD[w];
                PointD[] b = new PointD[w];
                PointD[] rf = new PointD[w];
                PointD[] gf = new PointD[w];
                PointD[] bf = new PointD[w];
                for (int x = 0; x < w; x++)
                {
                    if (x < lw && y < lh)
                    {
                        pixel = bitmap.GetPixel(x, y);
                        r[x].x = pixel.R;
                        g[x].x = pixel.G;
                        b[x].x = pixel.B;
                    }
                }
                Maths.FastFourierTransformation(r, rf, wp);
                Maths.FastFourierTransformation(g, gf, wp);
                Maths.FastFourierTransformation(b, bf, wp);

                for (int x = 0; x < w; x++)
                {
                    int r1 = (int)(Math.Sqrt(rf[x].x * rf[x].x + rf[x].y * rf[x].y) / 30);
                    int g1 = (int)(Math.Sqrt(gf[x].x * gf[x].x + gf[x].y * gf[x].y) / 30);
                    int b1 = (int)(Math.Sqrt(bf[x].x * bf[x].x + bf[x].y * bf[x].y) / 30);
                    if (r1 > 255)
                        r1 = 255; ;
                    if (g1 > 255)
                        g1 = 255;
                    if (b1 > 255)
                        b1 = 255;
                    bitmap1.SetPixel(x, y, Color.FromArgb(r1, g1, b1));
                }
            }
            bitmap.Dispose();
            return bitmap1;
        }

        /// <summary>
        /// Menus the item13_ click.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <returns></returns>
        public static Bitmap menuItem13_Click(UnsafeBitmap _bitmap)
        {
            int lw = _bitmap.Width;
            int lh = _bitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = _bitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));
                }
            }
            return bitmap;
        }

        /// <summary>
        /// Menus the item14_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <returns></returns>
        public static Bitmap menuItem14_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            long ji = h * w * 3;

            PointD[] pingyu = new PointD[h * w * 3];
            int m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            m = 0;
            Bitmap bitmap = new Bitmap(w, h);

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    int r = (int)(Math.Sqrt(pingyu[m].x * pingyu[m].x + pingyu[m].y * pingyu[m].y) / 100);
                    int g = (int)(Math.Sqrt(pingyu[m + 1].x * pingyu[m + 1].x + pingyu[m + 1].y * pingyu[m + 1].y) / 100);
                    int b = (int)(Math.Sqrt(pingyu[m + 2].x * pingyu[m + 2].x + pingyu[m + 2].y * pingyu[m + 2].y) / 100);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));
                    m += 3;
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Menus the item15_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <returns></returns>
        public static Bitmap menuItem15_Click(UnsafeBitmap MyBitmap)
        {
            double r, g, b;
            int TI, TJ;

            int i;
            int j;

            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;

            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            PointD[] FD = new PointD[w * h * 3];
            PointD[] TD = new PointD[w * h * 3];
            PointD[] TempD = new PointD[w * h * 3];
            Color pixel;

            for (i = 0; i < h; i++)
            {
                // ÁÐ
                for (j = 0; j < w; j++)
                {
                    if (i < lh && j < lw)
                    {
                        pixel = MyBitmap.GetPixel(j, i);
                        TD[i * w * 3 + j * 3].x = pixel.R;
                        TD[i * w * 3 + j * 3 + 1].x = pixel.G;
                        TD[i * w * 3 + j * 3 + 2].x = pixel.B;
                    }
                    else
                    {
                        TD[i * w * 3 + j * 3].x = 0;
                        TD[i * w * 3 + j * 3 + 1].x = 0;
                        TD[i * w * 3 + j * 3 + 2].x = 0;
                    }
                    TD[i * w * 3 + j * 3].y = 0;
                    TD[i * w * 3 + j * 3 + 1].y = 0;
                    TD[i * w * 3 + j * 3 + 2].y = 0;
                }
            }

            Maths.Fourier(TD, lw, lh, FD);

            Bitmap bitmap = new Bitmap(w, h);

            for (i = 0; i < h; i++)
            {
                for (j = 0; j < 3 * w; j++)
                {
                    if (i < h / 2)
                        TI = i + h / 2;
                    else
                        TI = i - h / 2;

                    if (j < w * 3 / 2)
                        TJ = j + 3 * w / 2;
                    else
                        TJ = j - 3 * w / 2;


                    TempD[i * w * 3 + j] = FD[TI * w * 3 + TJ];
                }
            }

            for (i = (int)(h - lh) / 2; i < (int)(h + lh) / 2; i++)
            {
                for (j = (int)(w - lw) / 2; j < (int)(w + lw) / 2; j++)
                {
                    r = Math.Sqrt(TempD[w * 3 * i + j * 3].x * TempD[w * 3 * i + j * 3].x +
                        TempD[w * 3 * i + j * 3].y * TempD[w * 3 * i + j * 3].y) / 100;
                    g = Math.Sqrt(TempD[w * 3 * i + j * 3 + 1].x * TempD[w * 3 * i + j * 3 + 1].x +
                        TempD[w * 3 * i + j * 3 + 1].y * TempD[w * 3 * i + j * 3 + 1].y) / 100;
                    b = Math.Sqrt(TempD[w * 3 * i + j * 3 + 2].x * TempD[w * 3 * i + j * 3 + 2].x +
                        TempD[w * 3 * i + j * 3 + 2].y * TempD[w * 3 * i + j * 3 + 2].y) / 100;

                    int dTemp = (int)(r + g + b) / 3;

                    if (dTemp > 255)
                    {
                        dTemp = 255;
                    }

                    TI = i - (h - lh) / 2;
                    TJ = j / 3 - (w - lw) / 2;

                    bitmap.SetPixel(j, i, Color.FromArgb(dTemp, dTemp, dTemp));
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Menus the item16_ click.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <returns></returns>
        public static Bitmap menuItem16_Click(UnsafeBitmap _bitmap)
        {
            int lw = _bitmap.Width;
            int lh = _bitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            int m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = _bitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }
            Maths.Fourier(tu, lw, lh, pingyu);
            m = 0;

            int TI, TJ;
            PointD[] TempD = new PointD[w * h * 3];
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < 3 * w; j++)
                {
                    if (i < h / 2)
                        TI = i + h / 2;
                    else
                        TI = i - h / 2;

                    if (j < w * 3 / 2)
                        TJ = j + 3 * w / 2;
                    else
                        TJ = j - 3 * w / 2;


                    TempD[i * w * 3 + j] = pingyu[TI * w * 3 + TJ];
                }
            }
            Bitmap bitmap = new Bitmap(w, h);
            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    int r = (int)(Math.Sqrt(TempD[m].x * TempD[m].x + TempD[m].y * TempD[m].y) / 100);
                    int g = (int)(Math.Sqrt(TempD[m + 1].x * TempD[m + 1].x + TempD[m + 1].y * TempD[m + 1].y) / 100);
                    int b = (int)(Math.Sqrt(TempD[m + 2].x * TempD[m + 2].x + TempD[m + 2].y * TempD[m + 2].y) / 100);
                    if (r > 255)
                        r = 255;
                    if (g > 255)
                        g = 255;
                    if (b > 255)
                        b = 255;

                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));

                    m += 3;
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Menus the item18_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <param name="nValue">The n value.</param>
        /// <returns></returns>
        public static Bitmap menuItem18_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;
            //Form4 basi = new Form4();
            //basi.Text = "°ÍÌØÎÖË¹µÍÍ¨ÂË²¨µ÷½Ú";

            fD = nValue;

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {
                    int k = (int)(j / 3);
                    fDN = (float)(i * i + k * k);

                    //FD[i * 3 * w + j] *= complex<double>(1 / (1 + 0.414 * (fDN / fD) * (fDN / fD)), 0.0f);
                    pingyu[i * 3 * w + j].x *= 1 / (1 + 0.414 * fDN / (fD * fD));
                    pingyu[i * 3 * w + j].y *= 1 / (1 + 0.414 * fDN / (fD * fD));
                }
            }

            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);


            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Menus the item19_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <param name="nValue">The n value.</param>
        /// <returns></returns>
        public static Bitmap menuItem19_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;

            if (nValue > 25 || nValue < 1)
            {
                return null;
            }

            /*

             * double H;
            for(int i = 0; i < h; i++)
            {
                for(int j = 0; j < w * 3; j++)
                {
                    int k = (int)(j / 3);
                    H = (double)(i*i+k*k) ; 
                    H = (fD* fD) / H ; 
                    H = 1/(1+H)            ;
                    //pCFData[y*nTransWidth + x]=complex<double>(H*(pCFData[y*nTransWidth + x].real()),
                    //	H*(pCFData[y*nTransWidth + x].imag())  );
                    pingyu[i * 3 * w + j].x*=H;
                    pingyu[i * 3 * w + j].y*=H;	
                }
            }*/

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    fDN = (float)Math.Sqrt(i * i + j * j);

                    //FD[i * 3 * w + j] *= complex<double>(1.0 / (1.0 + 0.414 * (fD / fDN) * (fD / fDN) + 0.5), 0.0f);
                    float mm = 1.0f / (1.0f + 0.414f * (fD / fDN) * (fD / fDN));
                    pingyu[i * 3 * w + 3 * j].x *= mm;
                    pingyu[i * 3 * w + 3 * j].y *= mm;
                    pingyu[i * 3 * w + 3 * j + 1].x *= mm;
                    pingyu[i * 3 * w + 3 * j + 1].y *= mm;
                    pingyu[i * 3 * w + 3 * j + 2].x *= mm;
                    pingyu[i * 3 * w + 3 * j + 2].y *= mm;
                }
            }

            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
            return bitmap;
        }

        public static Bitmap menuItem21_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;

            if (nValue > 250 || nValue < 0)
            {
                return null;
            }

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {
                    int k = (int)(j / 3);
                    fDN = (float)Math.Sqrt(i * i + k * k);
                    if (fDN < fD)
                    {
                        pingyu[i * 3 * w + j].x = 0;
                        pingyu[i * 3 * w + j].y = 0;
                    }
                }
            }

            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
            return bitmap;
        }

        public static void menuItem22_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);
            // µ±Ç°ÆµÂÊ
            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;
            if (nValue < 0 || nValue > 250)
            {
                return;
            }

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {

                    int k = (int)(j / 3);
                    fDN = (float)Math.Sqrt(i * i + k * k);
                    if (fDN > fD)
                    {
                        pingyu[i * 3 * w + j].x = 0;
                        pingyu[i * 3 * w + j].y = 0;
                    }
                }
            }

            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
        }

        public static void menuItem23_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {
                    int k = (int)(j / 3);
                    fDN = (float)Math.Sqrt(i * i + k * k);
                    float fH = 1.0f / (1.0f + 0.414f * (fDN / fD) * (fDN / fD));

                    if (fH > 0.25f)
                    {
                        pingyu[i * 3 * w + j].x /= fH;
                        pingyu[i * 3 * w + j].y /= fH;
                    }
                    else
                    {
                        pingyu[i * 3 * w + j].x *= 0.2f;
                        pingyu[i * 3 * w + j].y *= 0.2f;
                    }
                }
            }



            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
        }

        public static void menuItem24_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;


            float K = 0.05f;
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {

                    int k = (int)(j / 3);
                    fDN = (float)Math.Sqrt(i * i + k * k);

                    float fH = 1.0f / (1.0f + 0.414f * (fDN / fD) * (fDN / fD));

                    //FD[i * 3 * w + j] *= complex<double>(fH / (fH * fH + K), 0.0f);
                    pingyu[i * 3 * w + j].x *= fH / (fH * fH + K);
                    pingyu[i * 3 * w + j].y *= fH / (fH * fH + K);
                }
            }


            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
        }

        public static void menuItem25_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }
            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;


            float K = 0.05f;
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {
                    int k = (int)(j / 3);
                    fDN = (float)Math.Sqrt(i * i + k * k);


                    //FD[i * 3 * w + j] *= complex<double>(1 / sqrt(fH * fH + K), 0.0f);

                    float fH = 1.0f / (1.0f + 0.414f * (fDN / fD) * (fDN / fD));
                    pingyu[i * 3 * w + j].x *= 1 / Math.Sqrt(fH * fH + K);
                    pingyu[i * 3 * w + j].y *= 1 / Math.Sqrt(fH * fH + K);
                }
            }



            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
        }

        public static void menuItem26_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fDN = 0.0f, fD = 0.0f;

            fD = nValue;


            float S = 0.00003f;


            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {

                    int k = (int)(j / 3);
                    fDN = (float)Math.Sqrt(i * i + k * k);

                    float P = (float)(2.0f * Math.PI * fDN);

                    //FD[i * 3 * w + j] *= complex<double>(fH / (fH * fH + S * P), 0.0f);

                    float fH = 1.0f / (1.0f + 0.414f * (fDN / fD) * (fDN / fD));
                    pingyu[i * 3 * w + j].x *= fH / (fH * fH + S * P);
                    pingyu[i * 3 * w + j].y *= fH / (fH * fH + S * P);
                }
            }



            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
        }

        public static Bitmap menuItem30_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;

            double[] tu = new double[w * h * 3];
            double[] pingyu = new double[w * h * 3];

            int m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m] = pixel.R;
                        tu[m + 1] = pixel.G;
                        tu[m + 2] = pixel.B;
                    }
                    else
                    {
                        tu[m] = 0;
                        tu[m + 1] = 0;
                        tu[m + 2] = 0;
                    }
                    m = m + 3;
                }
            }

            Maths.FreqDCT(tu, pingyu, lw, lh);

            Bitmap bitmap = new Bitmap(w, h);
            //°´ÐÐÈ¡³öÊý×é
            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {
                    int r = (int)(Math.Abs(pingyu[x * w * 3 + 3 * y]));
                    int g = (int)(Math.Abs(pingyu[x * w * 3 + 3 * y + 1]));
                    int b = (int)(Math.Abs(pingyu[x * w * 3 + 3 * y + 2]));
                    if (r > 255)
                        r = 255;
                    if (g > 255)
                        g = 255;
                    if (b > 255)
                        b = 255;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));
                }
            }
            return bitmap;
        }

        public static Bitmap menuItem31_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;

            double[] tu = new double[w * h * 3];
            double[] pingyu = new double[w * h * 3];

            int m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m] = pixel.R;
                        tu[m + 1] = pixel.G;
                        tu[m + 2] = pixel.B;
                    }
                    else
                    {
                        tu[m] = 0;
                        tu[m + 1] = 0;
                        tu[m + 2] = 0;
                    }
                    m = m + 3;
                }
            }

            Maths.FreqDCT(tu, pingyu, lw, lh);
            double[] bian = new double[w * h * 3];
            Maths.IFreqDCT(bian, pingyu, lw, lh);

            Bitmap bitmap = new Bitmap(w, h);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {
                    int r = (int)(Math.Abs(bian[x * w * 3 + 3 * y]));
                    int g = (int)(Math.Abs(bian[x * w * 3 + 3 * y + 1]));
                    int b = (int)(Math.Abs(bian[x * w * 3 + 3 * y + 2]));
                    if (r > 255)
                        r = 255;
                    if (g > 255)
                        g = 255;
                    if (b > 255)
                        b = 255;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));
                }
            }
            return bitmap;
        }

        public static Bitmap Walsh(Bitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;

            double[] tu = new double[w * h * 3];
            double[] pingyu = new double[w * h * 3];

            int m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m] = pixel.R;
                        tu[m + 1] = pixel.G;
                        tu[m + 2] = pixel.B;
                    }
                    else
                    {
                        tu[m] = 0;
                        tu[m + 1] = 0;
                        tu[m + 2] = 0;
                    }
                    m = m + 3;
                }
            }

            Maths.FreqWALSH(tu, pingyu, lw, lh);

            Bitmap bitmap = new Bitmap(w, h);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {
                    int r = (int)(Math.Abs(pingyu[x * w * 3 + 3 * y] * 1000));
                    int g = (int)(Math.Abs(pingyu[x * w * 3 + 3 * y + 1] * 1000));
                    int b = (int)(Math.Abs(pingyu[x * w * 3 + 3 * y + 2] * 1000));
                    if (r > 255)
                        r = 255;
                    if (g > 255)
                        g = 255;
                    if (b > 255)
                        b = 255;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));
                }
            }
            return bitmap;
        }

        public static Bitmap menuItem34_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;

            double[] tu = new double[w * h * 3];
            double[] pingyu = new double[w * h * 3];

            int m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m] = pixel.R;
                        tu[m + 1] = pixel.G;
                        tu[m + 2] = pixel.B;
                    }
                    else
                    {
                        tu[m] = 0;
                        tu[m + 1] = 0;
                        tu[m + 2] = 0;
                    }
                    m = m + 3;
                }
            }

            Maths.FreqWALSH(tu, pingyu, lw, lh);
            double[] bian = new double[w * h * 3];
            Maths.IFreqWALSH(bian, pingyu, lw, lh);


            Bitmap bitmap = new Bitmap(w, h);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {
                    int r = (int)(Math.Abs(bian[x * w * 3 + 3 * y]));
                    int g = (int)(Math.Abs(bian[x * w * 3 + 3 * y + 1]));
                    int b = (int)(Math.Abs(bian[x * w * 3 + 3 * y + 2]));
                    if (r > 255)
                        r = 255;
                    if (g > 255)
                        g = 255;
                    if (b > 255)
                        b = 255;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b));
                }
            }
            return bitmap;
        }

        /// <summary>
        /// Menus the item35_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <param name="nValue">The n value.</param>
        /// <returns></returns>
        public static Bitmap menuItem35_Click(Bitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;
            PointD[] tu = new PointD[h * w * 3];
            PointD[] pingyu = new PointD[h * w * 3];
            long m = 0;

            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (x < lh && y < lw)
                    {
                        pixel = MyBitmap.GetPixel(y, x);
                        tu[m].x = pixel.R;
                        tu[m + 1].x = pixel.G;
                        tu[m + 2].x = pixel.B;
                    }
                    else
                    {
                        tu[m].x = 0;
                        tu[m + 1].x = 0;
                        tu[m + 2].x = 0;
                    }
                    tu[m].y = 0;
                    tu[m + 1].y = 0;
                    tu[m + 2].y = 0;
                    m += 3;
                }
            }

            Maths.Fourier(tu, lw, lh, pingyu);

            float fD = 0.0f;

            fD = nValue;

            if (nValue > 30 || nValue < 0)
            {
                return null;
            }


            double H;
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w * 3; j++)
                {
                    int k = (int)(j / 3);
                    H = (double)(i * i + k * k);
                    H = (fD * fD) / H;
                    H = 1 / (1 + H);
                    pingyu[i * 3 * w + j].x *= H;
                    pingyu[i * 3 * w + j].y *= H;
                }
            }



            Maths.IFourier(tu, lw, lh, pingyu);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(tu[(long)(w * 3 * x + 3 * y)].x * 2);
                    int g = (int)(tu[(long)(w * 3 * x) + 3 * y + 1].x * 2);
                    int b = (int)(tu[(long)(w * 3 * x) + 3 * y + 2].x * 2);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
            return bitmap;
        }

        /// <summary>
        /// Menus the item36_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <returns></returns>
        public static Bitmap menuItem36_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;
            long i, j;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }
            Color pixel;

            PointD[] pCTSrc = new PointD[h * w * 3];
            PointD[] pCTH = new PointD[h * w * 3];

            PointD[] pCFSrc = new PointD[h * w * 3];
            PointD[] pCFH = new PointD[h * w * 3];


            long m = 0;
            for (j = 0; j < h; j++)
            {
                for (i = 0; i < w; i++)
                {
                    if (j < lh && i < lw)
                    {
                        pixel = MyBitmap.GetPixel((int)i, (int)j);
                        pCTSrc[m].x = pixel.R;
                        pCTSrc[m + 1].x = pixel.G;
                        pCTSrc[m + 2].x = pixel.B;
                    }
                    else
                    {
                        pCTSrc[m].x = 0;
                        pCTSrc[m + 1].x = 0;
                        pCTSrc[m + 2].x = 0;
                    }
                    pCTSrc[m].y = 0;
                    pCTSrc[m + 1].y = 0;
                    pCTSrc[m + 2].y = 0;
                    pCFSrc[m].x = 0;
                    pCFSrc[m + 1].x = 0;
                    pCFSrc[m + 2].x = 0;
                    pCFSrc[m].y = 0;
                    pCFSrc[m + 1].y = 0;
                    pCFSrc[m + 2].y = 0;

                    if (i < 5 && j < 5)
                    {
                        pCTH[m].x = 0.04;
                        pCTH[m + 1].x = 0.04;
                        pCTH[m + 2].x = 0.04;
                        //pCTH[ lLineBytes*j + i ] = complex<double>(0.04 , 0.0);
                    }
                    else
                    {
                        pCTH[m].x = 0;
                        pCTH[m + 1].x = 0;
                        pCTH[m + 2].x = 0;
                        //pCTH[ lLineBytes*j + i ] = complex<double>(0.0 , 0.0);
                    }
                    pCTH[m].y = 0;
                    pCTH[m + 1].y = 0;
                    pCTH[m + 2].y = 0;
                    pCFH[m].x = 0;
                    pCFH[m + 1].x = 0;
                    pCFH[m + 2].x = 0;
                    pCFH[m].y = 0;
                    pCFH[m + 1].y = 0;
                    pCFH[m + 2].y = 0;

                    m += 3;
                }
            }

            Maths.Fourier(pCTSrc, lw, lh, pCFSrc);
            Maths.Fourier(pCTH, lw, lh, pCFH);


            PointD[] last = new PointD[h * w * 3];
            for (i = 0; i < h * w * 3; i++)
            {
                last[i].x = pCFSrc[i].x * pCFH[i].x - pCFSrc[i].y * pCFH[i].y;
                last[i].y = pCFSrc[i].y * pCFH[i].x + pCFSrc[i].x * pCFH[i].y;
            }



            Maths.IFourier(pCTSrc, lw, lh, last);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(pCTSrc[(long)(w * 3 * x + 3 * y)].x);
                    int g = (int)(pCTSrc[(long)(w * 3 * x) + 3 * y + 1].x);
                    int b = (int)(pCTSrc[(long)(w * 3 * x) + 3 * y + 2].x);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b)); ;
                }
            }
            return bitmap;
        }

        /// <summary>
        /// Menus the item37_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void menuItem37_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;
            long i, j;

            double a, b, c, d;


            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;

            PointD[] pCTSrc = new PointD[h * w * 3];
            PointD[] pCTH = new PointD[h * w * 3];

            PointD[] pCFSrc = new PointD[h * w * 3];
            PointD[] pCFH = new PointD[h * w * 3];


            long m = 0;
            for (j = 0; j < h; j++)
            {
                for (i = 0; i < w; i++)
                {
                    if (j < lh && i < lw)
                    {
                        pixel = MyBitmap.GetPixel((int)i, (int)j);
                        pCTSrc[m].x = pixel.R;
                        pCTSrc[m + 1].x = pixel.G;
                        pCTSrc[m + 2].x = pixel.B;
                    }
                    else
                    {
                        pCTSrc[m].x = 0;
                        pCTSrc[m + 1].x = 0;
                        pCTSrc[m + 2].x = 0;
                    }
                    pCTSrc[m].y = 0;
                    pCTSrc[m + 1].y = 0;
                    pCTSrc[m + 2].y = 0;
                    pCFSrc[m].x = 0;
                    pCFSrc[m + 1].x = 0;
                    pCFSrc[m + 2].x = 0;
                    pCFSrc[m].y = 0;
                    pCFSrc[m + 1].y = 0;
                    pCFSrc[m + 2].y = 0;

                    if (i < 5 && j < 5)
                    {
                        pCTH[m].x = 0.04;
                        pCTH[m + 1].x = 0.04;
                        pCTH[m + 2].x = 0.04;
                        //pCTH[ lLineBytes*j + i ] = complex<double>(0.04 , 0.0);
                    }
                    else
                    {
                        pCTH[m].x = 0;
                        pCTH[m + 1].x = 0;
                        pCTH[m + 2].x = 0;
                        //pCTH[ lLineBytes*j + i ] = complex<double>(0.0 , 0.0);
                    }
                    pCTH[m].y = 0;
                    pCTH[m + 1].y = 0;
                    pCTH[m + 2].y = 0;
                    pCFH[m].x = 0;
                    pCFH[m + 1].x = 0;
                    pCFH[m + 2].x = 0;
                    pCFH[m].y = 0;
                    pCFH[m + 1].y = 0;
                    pCFH[m + 2].y = 0;

                    m += 3;
                }
            }

            Maths.Fourier(pCTSrc, lw, lh, pCFSrc);

            Maths.Fourier(pCTH, lw, lh, pCFH);

            for (i = 0; i < h * w * 3; i++)
            {
                a = pCFSrc[i].x;
                b = pCFSrc[i].y;
                c = pCFH[i].x;
                d = pCFH[i].y;


                if (c * c + d * d > 1e-3)
                {
                    pCFSrc[i].x = (a * c + b * d) / (c * c + d * d);
                    pCFSrc[i].y = (b * c - a * d) / (c * c + d * d);
                }
            }



            Maths.IFourier(pCTSrc, lw, lh, pCFSrc);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    int r = (int)(pCTSrc[(long)(w * 3 * x + 3 * y)].x * 255.0 / 300);
                    int g = (int)(pCTSrc[(long)(w * 3 * x) + 3 * y + 1].x * 255.0 / 300);
                    int b1 = (int)(pCTSrc[(long)(w * 3 * x) + 3 * y + 2].x * 255.0 / 300);
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b1 > 255)
                        b1 = 255;
                    else if (b1 < 0)
                        b1 = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b1)); ;
                }
            }
        }

        /// <summary>
        /// Menus the item38_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void menuItem38_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            int w = 1;
            int h = 1;
            int wp = 0;
            int hp = 0;
            long i, j;

            double temp, a, b, c, d, norm2;

            while (w < lw)
            {
                w *= 2;
                wp++;
            }

            while (h < lh)
            {
                h *= 2;
                hp++;
            }

            Color pixel;

            PointD[] pCTSrc = new PointD[h * w * 3];
            PointD[] pCTH = new PointD[h * w * 3];

            PointD[] pCFSrc = new PointD[h * w * 3];
            PointD[] pCFH = new PointD[h * w * 3];

            double[] pCFFilter = new double[h * w * 3];

            long m = 0;
            for (j = 0; j < h; j++)
            {
                for (i = 0; i < w; i++)
                {
                    if (j < lh && i < lw)
                    {
                        pixel = MyBitmap.GetPixel((int)i, (int)j);
                        pCTSrc[m].x = pixel.R;
                        pCTSrc[m + 1].x = pixel.G;
                        pCTSrc[m + 2].x = pixel.B;
                    }
                    else
                    {
                        pCTSrc[m].x = 0;
                        pCTSrc[m + 1].x = 0;
                        pCTSrc[m + 2].x = 0;
                    }
                    pCTSrc[m].y = 0;
                    pCTSrc[m + 1].y = 0;
                    pCTSrc[m + 2].y = 0;
                    pCFSrc[m].x = 0;
                    pCFSrc[m + 1].x = 0;
                    pCFSrc[m + 2].x = 0;
                    pCFSrc[m].y = 0;
                    pCFSrc[m + 1].y = 0;
                    pCFSrc[m + 2].y = 0;

                    if (i < 5 && j < 5)
                    {
                        pCTH[m].x = 0.04;
                        pCTH[m + 1].x = 0.04;
                        pCTH[m + 2].x = 0.04;
                        pCFFilter[m] = 0.5;
                        pCFFilter[m + 1] = 0.5;
                        pCFFilter[m + 2] = 0.5;
                        //pCTH[ lLineBytes*j + i ] = complex<double>(0.04 , 0.0);
                    }
                    else
                    {
                        pCTH[m].x = 0;
                        pCTH[m + 1].x = 0;
                        pCTH[m + 2].x = 0;
                        pCFFilter[m] = 0.05;
                        pCFFilter[m + 1] = 0.05;
                        pCFFilter[m + 2] = 0.05;
                        //pCTH[ lLineBytes*j + i ] = complex<double>(0.0 , 0.0);
                    }
                    pCTH[m].y = 0;
                    pCTH[m + 1].y = 0;
                    pCTH[m + 2].y = 0;
                    pCFH[m].x = 0;
                    pCFH[m + 1].x = 0;
                    pCFH[m + 2].x = 0;
                    pCFH[m].y = 0;
                    pCFH[m + 1].y = 0;
                    pCFH[m + 2].y = 0;

                    m += 3;
                }
            }

            Maths.Fourier(pCTSrc, lw, lh, pCFSrc);
            Maths.Fourier(pCTH, lw, lh, pCFH);

            for (i = 0; i < w * h * 3; i++)
            {

                a = pCFSrc[i].x;
                b = pCFSrc[i].y;
                c = pCFH[i].x;
                d = pCFH[i].y;

                // |H(u,v)|*|H(u,v)|
                norm2 = c * c + d * d;

                // |H(u,v)|*|H(u,v)|/(|H(u,v)|*|H(u,v)|+a)
                temp = (norm2) / (norm2 + pCFFilter[i]);

                {
                    pCFSrc[i].x = temp * (a * c + b * d) / (c * c + d * d);
                    pCFSrc[i].y = temp * (b * c - a * d) / (c * c + d * d);
                }
            }


            Maths.IFourier(pCTSrc, lw, lh, pCFSrc);

            Bitmap bitmap = new Bitmap(lw, lh);

            for (int x = 0; x < lh; x++)
            {
                for (int y = 0; y < lw; y++)
                {

                    a = (int)(pCTSrc[(long)(w * 3 * x + 3 * y)].x);
                    b = (int)(pCTSrc[(long)(w * 3 * x + 3 * y)].x);
                    norm2 = a * a + b * b;
                    int r = (int)Math.Sqrt(norm2) + 40;

                    a = (int)(pCTSrc[(long)(w * 3 * x + 3 * y + 1)].x);
                    b = (int)(pCTSrc[(long)(w * 3 * x + 3 * y + 1)].x);
                    norm2 = a * a + b * b;
                    int g = (int)Math.Sqrt(norm2) + 40;

                    a = (int)(pCTSrc[(long)(w * 3 * x + 3 * y + 2)].x);
                    b = (int)(pCTSrc[(long)(w * 3 * x + 3 * y + 2)].x);
                    norm2 = a * a + b * b;
                    int b1 = (int)Math.Sqrt(norm2) + 40;

                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b1 > 255)
                        b1 = 255;
                    else if (b1 < 0)
                        b1 = 0;
                    bitmap.SetPixel(y, x, Color.FromArgb(r, g, b1));
                }
            }
        }

        /// <summary>
        /// Menus the item40_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void menuItem40_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;

            Color pixel;

            int i, j;
            int[] yefeng = new int[lw * lh];

            for (j = 0; j < lh; j++)
                for (i = 0; i < lw; i++)
                {
                    pixel = MyBitmap.GetPixel(i, j);
                    yefeng[j * lw + i] = (2 * pixel.R + 7 * pixel.G + pixel.B) / 10;
                }

            Maths.DiscreteWaveletTransform(yefeng, lw, lh);

            Bitmap bitmap = new Bitmap(lw, lh);
            int hui;
            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {

                    hui = yefeng[j * lw + i];
                    if (j > lh / 2 || i > lw / 2)
                        hui = 80 + hui * 4;

                    if (hui > 255)
                        hui = 255;
                    else if (hui < 0)
                        hui = 0;
                    bitmap.SetPixel(i, j, Color.FromArgb(hui, hui, hui));
                }
            }
        }

        /// <summary>
        /// Wavelets the transform.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void WaveletTransform(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;

            Color pixel;

            int i, j;
            int[] yefeng = new int[lw * lh];

            for (j = 0; j < lh; j++)
                for (i = 0; i < lw; i++)
                {
                    pixel = MyBitmap.GetPixel(i, j);
                    yefeng[j * lw + i] = (2 * pixel.R + 7 * pixel.G + pixel.B) / 10;
                }

            Maths.DiscreteWaveletTransform(yefeng, lw, lh);
            Maths.InverseDicreteWaveletTransform(yefeng, lw, lh);

            Bitmap bitmap = new Bitmap(lw, lh);
            int hui;
            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {

                    hui = yefeng[j * lw + i];

                    if (hui > 255)
                        hui = 255;
                    else if (hui < 0)
                        hui = 0;
                    bitmap.SetPixel(i, j, Color.FromArgb(hui, hui, hui));
                }
            }
        }

        /// <summary>
        /// Menus the item43_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void menuItem43_Click(Bitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;

            Color pixel;

            int i, j;
            int[] yefeng = new int[lw * lh * 3];

            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {
                    pixel = MyBitmap.GetPixel(i, j);
                    yefeng[j * lw * 3 + i * 3] = pixel.R;
                    yefeng[j * lw * 3 + i * 3 + 1] = pixel.G;
                    yefeng[j * lw * 3 + i * 3 + 2] = pixel.B;
                }
            }

            Maths.DWT1(yefeng, lw, lh);

            Bitmap bitmap = new Bitmap(lw, lh);
            int r, g, b;
            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {

                    r = yefeng[j * lw * 3 + i * 3];
                    g = yefeng[j * lw * 3 + i * 3 + 1];
                    b = yefeng[j * lw * 3 + i * 3 + 2];
                    if (j > lh / 2 || i > lw / 2)
                    {
                        r *= 10;
                        g *= 10;
                        b *= 10;
                    }

                    if (j > lh / 2 && i > lw / 2)
                    {
                        r *= 10;
                        g *= 10;
                        b *= 10;
                    }
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));
                }
            }
        }

        /// <summary>
        /// Menus the item44_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void menuItem44_Click(UnsafeBitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;

            Color pixel;

            int i, j;
            int[] yefeng = new int[lw * lh * 3];

            for (j = 0; j < lh; j++)
                for (i = 0; i < lw; i++)
                {
                    pixel = MyBitmap.GetPixel(i, j);
                    yefeng[j * lw * 3 + i * 3] = pixel.R;
                    yefeng[j * lw * 3 + i * 3 + 1] = pixel.G;
                    yefeng[j * lw * 3 + i * 3 + 2] = pixel.B;
                }

            Maths.DWT1(yefeng, lw, lh);

            Maths.IDWT1(yefeng, lw, lh);

            Bitmap bitmap = new Bitmap(lw, lh);
            int r, g, b;
            for (j = 0; j < lh; j++)
                for (i = 0; i < lw; i++)
                {

                    r = yefeng[j * lw * 3 + i * 3];
                    g = yefeng[j * lw * 3 + i * 3 + 1];
                    b = yefeng[j * lw * 3 + i * 3 + 2];
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));
                }

        }

        /// <summary>
        /// Menus the item45_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <param name="nValue">The n value.</param>
        public static void menuItem45_Click(UnsafeBitmap MyBitmap, int nValue)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            Color pixel;

            int i, j;
            int[] yefeng = new int[lw * lh * 3];

            for (j = 0; j < lh; j++)
                for (i = 0; i < lw; i++)
                {
                    pixel = MyBitmap.GetPixel(i, j);
                    yefeng[j * lw * 3 + i * 3] = pixel.R;
                    yefeng[j * lw * 3 + i * 3 + 1] = pixel.G;
                    yefeng[j * lw * 3 + i * 3 + 2] = pixel.B;
                }

            Maths.DWT1(yefeng, lw, lh);
            /*Form4 xiao = new Form4();
            xiao.Text = "Ð¡²¨±ä»»µÄ²ÎÊýµ÷½Ú";
            xiao.trackBar1.Value = 30;
            xiao.textBox1.Text = "30";
            xiao.trackBar1.Maximum = 200;
            xiao.trackBar1.Minimum = 0;*/

            float fD = 0.0f;

            if (nValue <= 0 || nValue >= 200)
            {
                return;
            }
            fD = nValue / 10.0f;

            for (j = lh / 2; j < lh; j++)
                for (i = lh / 2; i < lw; i++)
                {
                    yefeng[j * lw * 3 + i * 3] = (int)(yefeng[j * lw * 3 + i * 3] * fD);
                    yefeng[j * lw * 3 + i * 3 + 1] = (int)(yefeng[j * lw * 3 + i * 3 + 1] * fD);
                    yefeng[j * lw * 3 + i * 3 + 2] = (int)(yefeng[j * lw * 3 + i * 3 + 2] * fD);
                }
            Maths.IDWT1(yefeng, lw, lh);

            Bitmap bitmap = new Bitmap(lw, lh);
            int r, g, b;
            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {

                    r = yefeng[j * lw * 3 + i * 3];
                    g = yefeng[j * lw * 3 + i * 3 + 1];
                    b = yefeng[j * lw * 3 + i * 3 + 2];
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));
                }
            }
        }

        /// <summary>
        /// Discretes the wavelet transform.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        /// <param name="ll">The ll.</param>
        /// <param name="lh1">The LH1.</param>
        /// <param name="hl">The hl.</param>
        /// <param name="hh">The hh.</param>
        public static void DiscreteWaveletTransform(UnsafeBitmap MyBitmap, int ll, int lh1, int hl, int hh)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;
            Color pixel;

            int i, j;
            int[] yefeng = new int[lw * lh * 3];

            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {
                    pixel = MyBitmap.GetPixel(i, j);
                    yefeng[j * lw * 3 + i * 3] = pixel.R;
                    yefeng[j * lw * 3 + i * 3 + 1] = pixel.G;
                    yefeng[j * lw * 3 + i * 3 + 2] = pixel.B;
                }
            }

            Maths.DWT1(yefeng, lw, lh);

            for (j = 0; j < lh / 2; j++)
            {
                for (i = 0; i < lw / 2; i++)
                {
                    yefeng[j * lw * 3 + i * 3] = (yefeng[j * lw * 3 + i * 3] * ll) / 10;
                    yefeng[j * lw * 3 + i * 3 + 1] = (yefeng[j * lw * 3 + i * 3 + 1] * ll) / 10;
                    yefeng[j * lw * 3 + i * 3 + 2] = (yefeng[j * lw * 3 + i * 3 + 2] * ll) / 10;
                }
            }
            for (j = lh / 2; j < lh; j++)
            {
                for (i = 0; i < lw / 2; i++)
                {
                    yefeng[j * lw * 3 + i * 3] = (yefeng[j * lw * 3 + i * 3] * lh1) / 10;
                    yefeng[j * lw * 3 + i * 3 + 1] = (yefeng[j * lw * 3 + i * 3 + 1] * lh1) / 10;
                    yefeng[j * lw * 3 + i * 3 + 2] = (yefeng[j * lw * 3 + i * 3 + 2] * lh1) / 10;
                }
            }
            for (j = 0; j < lh / 2; j++)
            {
                for (i = lh / 2; i < lw; i++)
                {
                    yefeng[j * lw * 3 + i * 3] = (yefeng[j * lw * 3 + i * 3] * hl) / 10;
                    yefeng[j * lw * 3 + i * 3 + 1] = (yefeng[j * lw * 3 + i * 3 + 1] * hl) / 10;
                    yefeng[j * lw * 3 + i * 3 + 2] = (yefeng[j * lw * 3 + i * 3 + 2] * hl) / 10;
                }
            }
            for (j = lh / 2; j < lh; j++)
            {
                for (i = lh / 2; i < lw; i++)
                {
                    yefeng[j * lw * 3 + i * 3] = (yefeng[j * lw * 3 + i * 3] * hh) / 10;
                    yefeng[j * lw * 3 + i * 3 + 1] = (yefeng[j * lw * 3 + i * 3 + 1] * hh) / 10;
                    yefeng[j * lw * 3 + i * 3 + 2] = (yefeng[j * lw * 3 + i * 3 + 2] * hh) / 10;
                }
            }

            Maths.IDWT1(yefeng, lw, lh);

            Bitmap bitmap = new Bitmap(lw, lh);
            int r, g, b;
            for (j = 0; j < lh; j++)
            {
                for (i = 0; i < lw; i++)
                {

                    r = yefeng[j * lw * 3 + i * 3];
                    g = yefeng[j * lw * 3 + i * 3 + 1];
                    b = yefeng[j * lw * 3 + i * 3 + 2];
                    if (r > 255)
                        r = 255;
                    else if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    else if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    else if (b < 0)
                        b = 0;
                    bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));
                }
            }
        }

        /// <summary>
        /// Menus the item47_ click.
        /// </summary>
        /// <param name="MyBitmap">My bitmap.</param>
        public static void menuItem47_Click(Bitmap MyBitmap)
        {
            int lw = MyBitmap.Width;
            int lh = MyBitmap.Height;

            int i, j;

            int lMaxRange;
            if (lw > lh)
                lMaxRange = lw;
            else
                lMaxRange = lh;



            long AverEx;
            long AverEy;


            long ToaCount;


            double[,] Matr4C = new double[2, 2];


            double[,] QMatrix = new double[2, 2];


            double[] MainCross = new double[2];
            double[] HypoCross = new double[2];


            double dTemp;
            long lTempI;
            long lTempJ;


            AverEx = 0;
            AverEy = 0;
            ToaCount = 0;
            Matr4C[0, 0] = Matr4C[0, 1] = Matr4C[1, 0] = Matr4C[1, 1] = 0.0;


            double[] F = new double[lw * lh];

            Color pixel;


            for (i = 0; i < lh; i++)
            {

                for (j = 0; j < lw; j++)
                {

                    F[i * lw + j] = 255;


                    pixel = MyBitmap.GetPixel(i, j);

                    int m = (2 * pixel.R + 7 * pixel.G + pixel.B) / 10;


                    if (m < 255)
                    {

                        AverEx = AverEx + i;
                        AverEy = AverEy + j;


                        ToaCount++;


                        Matr4C[0, 0] = Matr4C[0, 0] + i * i;
                        Matr4C[0, 1] = Matr4C[0, 1] + i * j;
                        Matr4C[1, 0] = Matr4C[1, 0] + j * i;
                        Matr4C[1, 1] = Matr4C[1, 1] + j * j;
                    }
                }
            }


            AverEx = AverEx / ToaCount;
            AverEy = AverEy / ToaCount;


            Matr4C[0, 0] = Matr4C[0, 0] / ToaCount - AverEx * AverEx;
            Matr4C[0, 1] = Matr4C[0, 1] / ToaCount - AverEx * AverEy;
            Matr4C[1, 0] = Matr4C[1, 0] / ToaCount - AverEx * AverEy;
            Matr4C[1, 1] = Matr4C[1, 1] / ToaCount - AverEy * AverEy;



            double Eps = 0.000001;


            Maths.THREECROSS(Matr4C, QMatrix, MainCross, HypoCross);


            Maths.BSTQ(MainCross, HypoCross, Matr4C, Eps, 50);


            dTemp = Matr4C[0, 1];
            Matr4C[0, 0] = dTemp;


            for (i = 0; i <= 1; i++)
            {
                dTemp = Math.Pow(Matr4C[i, 0], 2) + Math.Pow(Matr4C[i, 1], 2);
                dTemp = Math.Sqrt(dTemp);
                Matr4C[i, 0] = Matr4C[i, 0] / dTemp;
                Matr4C[i, 1] = Matr4C[i, 1] / dTemp;
            }


            for (i = -lMaxRange + 1; i < lMaxRange; i++)
            {
                for (j = -lMaxRange + 1; j < lMaxRange; j++)
                {

                    int Cx = (int)(i * Matr4C[0, 0] - j * Matr4C[0, 1] + AverEx);
                    int Cy = (int)(-i * Matr4C[1, 0] + j * Matr4C[1, 1] + AverEy);


                    if (Cx >= 0 && Cx < lh && Cy >= 0 && Cy < lw)
                    {
                        //lpSrc = lpDIBBits + lLineBytes*Cx + Cy;
                        pixel = MyBitmap.GetPixel(Cx, Cy);
                        int m = (2 * pixel.R + 7 * pixel.G + pixel.B) / 10;


                        lTempI = (long)(lh / 2) + j;
                        lTempJ = (long)(lw / 2) + i;


                        if (lTempI >= 0 && lTempI < lh && lTempJ >= 0 && lTempJ < lw)
                            F[lTempJ + (lTempI) * lw] = m;
                    }
                }
            }
            Bitmap bitmap = new Bitmap(lMaxRange, lMaxRange);

            for (i = 0; i < lMaxRange; i++)
            {
                for (j = 0; j < lMaxRange; j++)
                {
                    int m = (int)F[i * lMaxRange + j];
                    bitmap.SetPixel(lh - 1 - i, j, Color.FromArgb(m, m, m));
                }
            }

        }


        /// <summary>
        /// Finds the B rectangles.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <returns></returns>
        public static List<ColorRect> FindBRectangles(UnsafeBitmap _bitmap)
        {
            int nXStart = -1;
            int nXEnd = -1;

            UnsafeBitmap bitmap = _bitmap;

            int nStartY = -1;
            int nEndY = -1;
            int nEndX = -1;
            int nStartX = -1;

            List<bool> aLine = new List<bool>();
            List<Color> aColor = new List<Color>();
            aColor.Add(bitmap.GetPixel(0, 0));

            List<Color> aBackgroundColor = new List<Color>();
            for (int x = 0; x < bitmap.Width; x++)
            {
                aBackgroundColor.Add(bitmap.GetPixel(x, 0));

            }
            Color colorBackground = ColorFunctions.AverageColor(aBackgroundColor);

            nXStart = 0;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            for (int y = 0; y < nHeight; y++)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);
                    if (!ColorFunctions.IsInColorTolerance(colorCurrent, colorBackground, 10, 10, 10))
                    {
                        aLine.Add(true);
                        nStartY = y;
                        nStartX = x;
                        break;
                    }
                    else
                    {
                        aLine.Add(false);
                    }
                }
            }
            for (int y = nHeight - 1; y >= 0; y--)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);
                    if (colorCurrent != Color.FromArgb(255, Color.White))
                    {
                        aLine.Add(true);
                        nEndY = y;
                        nEndX = x;
                        break;
                    }
                    else
                    {
                        aLine.Add(false);
                    }
                }
            }

            Rectangle rectPerson = RectangleHelpers.CalcRectangle(new Point(nStartX, nStartY), new Point(nEndX, nEndY));

            int nXBeltStart = -1;
            int nXBeltEnd = -1;
            int nYBeltStart = -1;
            int nYBeltEnd = -1;
            int nLineCount = 0;
            for (int y = 0; y < nHeight; y++)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);

                    float fLum = ColorFunctions.GetLuminance(colorCurrent);
                    if (fLum < 30)
                    {
                        aLine.Add(true);
                        nXBeltStart = x;
                        nYBeltStart = y;
                    }
                }
                nLineCount += (aLine.Count > 0 ? 1 : 0);
            }

            for (int y = nHeight - 1; y >= 0; y--)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);
                    if (colorCurrent != Color.FromArgb(255, Color.White))
                    {
                        aLine.Add(true);
                        nYBeltEnd = y;
                        nXBeltEnd = x;
                        break;
                    }
                    else
                    {
                        aLine.Add(false);
                    }
                }
            }

            Rectangle rectBelt = RectangleHelpers.CalcRectangle(new Point(nXBeltStart, nYBeltStart), new Point(nXBeltEnd, nYBeltEnd));
            float fPercentage = rectBelt.Height / rectPerson.Height;

            List<ColorRect> aColorRect = new List<ColorRect>();
            aColorRect.Add(new ColorRect(Color.Black, rectBelt));
            aColorRect.Add(new ColorRect(Color.Red, rectPerson));

            bitmap.Dispose();
            MessageBox.Show(fPercentage.ToString());

            return aColorRect;
        }

        /// <summary>
        /// Finds the B rectangles2.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <param name="fPercentage">The f percentage.</param>
        /// <returns></returns>
        public static List<ColorRect> FindBRectangles2(Bitmap _bitmap, out float fPercentage)
        {
            new SimpleSegmentation().Execute(_bitmap);
            int nXStart = -1;
            int nXEnd = -1;

            UnsafeBitmap bitmap = _bitmap;

            int nStartY = -1;
            int nEndY = -1;
            int nEndX = -1;
            int nStartX = -1;

            List<bool> aLine = new List<bool>();
            List<Color> aColor = new List<Color>();
            aColor.Add(bitmap.GetPixel(0, 0));

            List<Color> aBackgroundColor = new List<Color>();

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;
            for (int x = 0; x < nWidth; x++)
            {
                aBackgroundColor.Add(bitmap.GetPixel(x, 0));

            }
            Color colorBackground = ColorFunctions.AverageColor(aBackgroundColor);

            nXStart = 0;
            for (int y = 0; y < nHeight; y++)
            {
                aLine.Clear();
                for (int x = 0; x < bitmap.Width; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);
                    if (!ColorFunctions.IsInColorTolerance(colorCurrent, colorBackground, 10, 10, 10))
                    {
                        aLine.Add(true);
                        nStartY = y;
                        nStartX = x;
                        break;
                    }
                    else
                    {
                        aLine.Add(false);
                    }
                }
            }
            for (int y = nHeight - 1; y >= 0; y--)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);
                    if (colorCurrent != Color.FromArgb(255, Color.White))
                    {
                        aLine.Add(true);
                        nEndY = y;
                        nEndX = x;
                        break;
                    }
                    else
                    {
                        aLine.Add(false);
                    }
                }
            }

            Rectangle rectPerson = RectangleHelpers.CalcRectangle(new Point(nStartX, nStartY), new Point(nEndX, nEndY));

            int nXBeltStart = -1;
            int nXBeltEnd = -1;
            int nYBeltStart = -1;
            int nYBeltEnd = -1;
            int nLineCount = 0;
            for (int y = 0; y < nHeight; y++)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);

                    float fLum = ColorFunctions.GetLuminance(colorCurrent);
                    if (fLum < 30)
                    {
                        aLine.Add(true);
                        nXBeltStart = x;
                        nYBeltStart = y;
                    }
                }
                nLineCount += (aLine.Count > 0 ? 1 : 0);
            }

            for (int y = nHeight - 1; y >= 0; y--)
            {
                aLine.Clear();
                for (int x = 0; x < nWidth; x++)
                {
                    Color colorCurrent = bitmap.GetPixel(x, y);
                    aColor.Add(colorCurrent);
                    if (colorCurrent != Color.FromArgb(255, Color.White))
                    {
                        aLine.Add(true);
                        nYBeltEnd = y;
                        nXBeltEnd = x;
                        break;
                    }
                    else
                    {
                        aLine.Add(false);
                    }
                }
            }

            Rectangle rectBelt = RectangleHelpers.CalcRectangle(new Point(nXBeltStart, nYBeltStart), new Point(nXBeltEnd, nYBeltEnd));
            fPercentage = rectBelt.Height / rectPerson.Height;

            List<ColorRect> aColorRect = new List<ColorRect>();
            aColorRect.Add(new ColorRect(Color.Green, rectBelt));
            aColorRect.Add(new ColorRect(Color.Red, rectPerson));

            bitmap.Dispose();

            return aColorRect;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        public static void FindBelt1(Bitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            List<Color> aColor = new List<Color>();
            aColor.Add(bitmap.GetPixel(0, 0));

            Color colorLeft = Color.Transparent;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    colorLeft = bitmap.GetPixel(x, y);
                    if (ColorFunctions.GetLuminance(colorLeft) < 30)
                    {
                        while (x < nWidth)
                        {
                            Color color = bitmap.GetPixel(x, y);
                            bitmap.SetPixel(x, y, Color.Black);
                            x++;
                        }
                    }
                }
            }
            bitmap.Dispose();
        }
    }
}
